from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, QueryDict
from NoticeManage.models import *


# Create your views here.
@csrf_exempt
def sendNotice(request):
    if request.method == 'POST':
        print(request.POST.get)
        subjects = request.POST.get('subjects')
        title = request.POST.get('title')
        NoticeContent = request.POST.get('NoticeContent')
        check = Notice.objects.filter(subjects=subjects, title=title, NoticeContent=NoticeContent).count()
        if check == 0:
            notice = Notice()
            notice.subjects = subjects
            notice.title = title
            notice.NoticeContent = NoticeContent
            notice.save()
            return JsonResponse({"Message": "send notice success"}, safe=False)
        else:
            return JsonResponse({"Message": "this notice is sent already"}, safe=False)


@csrf_exempt
def getNotice(request):
    if request.method == 'GET':
        print('GET')
        notice_list = Notice.objects.all()
        notices = []
        for notice in notice_list:
            notices.append(notice.to_dict4())
        return JsonResponse({"notices": notices}, safe=False)


